package pl.smath.expression.model.presentation;

import pl.smath.expression.model.NumberTerm;
import pl.smath.expression.model.Term;
import pl.smath.expression.model.VariableTerm;

public class AtomTerm extends PresentationTerm {

	private final Term base;
	private final Term superscript;
	private final Term subscript;
	
	public AtomTerm(Term base, Term subscript, Term superscript) {
		super("");
		this.base = base;
		this.superscript = superscript;
		this.subscript = subscript;
	}
	
	/**
	 * Pobranie podstawy
	 */
	public Term getBase(){
		return base;
	}

	/**
	 * Pobranie indeksu g�rnego
	 */
	public Term getSuperscript(){
		return superscript;
	}

	/**
	 * Pobranie podstawy
	 */
	public Term getSubscript(){
		return subscript;
	}
	
	@Override
	public Term copy(){
		Term term = new AtomTerm(getBase().copy(), getSuperscript().copy(), getSubscript().copy());
		term.setNegative(isNegative());
		return term;
	}

	/**
	 * Sprawdzenie czy 2 wyrazenia sa sobie równowazne.
	 * Czyli czy mozna postawic miedzy nimi znak = (w sensie matematycznym)
	 */
	@Override
	public boolean isAbsTheSame(Term term){
		
		if(term instanceof AtomTerm){
			AtomTerm et = (AtomTerm)term;
			return getBase().isAbsTheSame(et.getBase()) && getSuperscript().isAbsTheSame(et.getSuperscript()) && getSubscript().isAbsTheSame(et.getSubscript());
		}
		
		return false;
	}
	
	public String toString(){
		String output = "";
		
		if(isNegative())
			output = "-";
		
		if( !(base instanceof NumberTerm || base instanceof VariableTerm) || base.isNegative()){
			output += "(" + base.toString() + ")";
		}
		else{
			output += base.toString();
		}
		
		output += "_";
	
		if( !(subscript instanceof NumberTerm || subscript instanceof VariableTerm) || subscript.isNegative()){
			output += "(" + subscript.toString() + ")";
		}
		else{
			output += subscript.toString();
		}

		
		output += "^";
	
		if( !(superscript instanceof NumberTerm || superscript instanceof VariableTerm) || superscript.isNegative()){
			output += "(" + superscript.toString() + ")";
		}
		else{
			output += superscript.toString();
		}
		
		return output;
		
	}

}
